package com.infisight.hudprojector.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.infisight.hudprojector.R;
import com.infisight.hudprojector.data.DataCmdMessageClass;
import com.infisight.hudprojector.data.ObdCmdsConfigClass;
import com.infisight.hudprojector.data.PMessage;
import com.infisight.hudprojector.data.SpeechVoiceData;
import com.infisight.hudprojector.data.VoiceDataClass;
import com.infisight.hudprojector.kdxfspeech.SpeechVoiceCompound;
import com.infisight.hudprojector.kdxfspeech.SpeechVoiceRecognition;
import com.infisight.hudprojector.service.MusicService;
import com.infisight.hudprojector.util.CommonUtil;
import com.infisight.hudprojector.util.Constants;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 显示HUD的Fragment
 * 
 * @author Administrator
 * 
 */
public class OBDFragment extends Fragment {
	protected static final String TAG = "OBDFragment";
	String TAG2 = "TAG2";
	SpeechVoiceCompound svc;
	SpeechVoiceRecognition svr;
	Gson gson;
	TextView tv_noobdinfo;
	LinearLayout ll_left, ll_right;
	int CONTROLID = 10001;
	HashMap<String, Integer> hmControlDes = new HashMap<String, Integer>();
	SharedPreferences sp;
	EditText txt_inputtest;

	/**
	 * 创建唯一实例
	 * 
	 * @return
	 */
	public static OBDFragment newInstance(String hudInfo) {
		OBDFragment fragment = new OBDFragment();
		Bundle args = new Bundle();
		args.putString("hudInfo", hudInfo);
		fragment.setArguments(args);
		return fragment;
	}

	/**
	 * 构造函数
	 */
	public OBDFragment() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 获取参数
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_obd, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		InitView();
		sp = getActivity().getSharedPreferences("bg_music_info", 0);
	}

	private void InitView() {
		tv_noobdinfo = (TextView) getActivity().findViewById(R.id.tv_noobdinfo);// 提示没有OBD数据信息
		ll_left = (LinearLayout) getActivity().findViewById(R.id.ll_left);// 左列的OBD数据信息
		ll_right = (LinearLayout) getActivity().findViewById(R.id.ll_right);// 右列的OBD数据信息
		txt_inputtest = (EditText)getActivity().findViewById(R.id.txt_inputtest);
		txt_inputtest.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus)
				{
					//向手机端发送广播
					processBroadcast();
				}
			}
		});
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		getActivity().unregisterReceiver(mObdReceiver);
		super.onPause();
		if (svc != null)
			svc.destroy();
		if (svr != null)
			svc.destroy();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		getActivity().registerReceiver(mObdReceiver, makeIntentFilter());
		super.onResume();
		svr = new SpeechVoiceRecognition(getActivity(), "", null);// 语音识别
		svc = SpeechVoiceCompound.getInstance(getActivity(), "", svr);// 语音合成
	}

	private static IntentFilter makeIntentFilter() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constants.OBD_HUD_DATA);
		intentFilter.addAction(Constants.MAIN_RETURN_HOME_ACTION);
		intentFilter.addAction(Constants.MAIN_NAVI_ACTION);
		intentFilter.addAction(Constants.MAIN_PHONE_ACTION);
		intentFilter.addAction(Constants.COMMON_UTIL_ACTION);
		intentFilter.addAction(Constants.MAIN_OBD_ACTION);
		intentFilter.addAction(Constants.MAIN_MUSIC_ACTION);
		intentFilter.addAction(Constants.INPUT_DATA);
		return intentFilter;
	}

	// 如果数据已经改变，就重绘制，否则，就不再重新绘制了
	private BroadcastReceiver mObdReceiver = new BroadcastReceiver() {

		@SuppressLint("ResourceAsColor")
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			final String action = intent.getAction();
			gson = new Gson();
			SpeechVoiceData svd;
			VoiceDataClass vdc;
			try {
				// 显示手机端的obd
				if (Constants.OBD_HUD_DATA.equals(action)) {
					String strMsg = intent.getStringExtra(Constants.MSG_PARAM);
					int position = 0;
					Gson gson = new Gson();
					List<ObdCmdsConfigClass> lstObd = new ArrayList<ObdCmdsConfigClass>();
					try {
						lstObd = gson.fromJson(strMsg,
								new TypeToken<List<ObdCmdsConfigClass>>() {
								}.getType());
					} catch (Exception e) {
					}
					if (lstObd.size() > 0) {
						CONTROLID = 10001;
						tv_noobdinfo.setVisibility(View.GONE);
						ll_left.setVisibility(View.VISIBLE);
						if (lstObd.size() > 1) {
							ll_right.setVisibility(View.VISIBLE);
						}
					} else {
						tv_noobdinfo.setVisibility(View.VISIBLE);
						ll_right.setVisibility(View.GONE);
					}
					for (ObdCmdsConfigClass occc : lstObd) {
						if (position % 2 == 0) {
							updateOBDView(true, CONTROLID, occc.getData(),
									occc.getDesc());
						} else {
							updateOBDView(false, CONTROLID, occc.getData(),
									occc.getDesc());
						}
						position++;
						CONTROLID++;
					}
				}
				// 返回首页
				else if (action != null
						&& Constants.MAIN_RETURN_HOME_ACTION.equals(action)) {
					Log.i(TAG, action + "action");
					CommonUtil.retHomeFragment(getActivity());
				}
				// obd界面打电话
				else if (action != null
						&& Constants.MAIN_PHONE_ACTION.equals(action)) {

					svd = gson.fromJson(
							intent.getStringExtra(Constants.MAIN_PHONE_ACTION),
							SpeechVoiceData.class);
					vdc = gson.fromJson(svd.getValue(), VoiceDataClass.class);

					svd.setCommand(Constants.MAIN_PHONE_COM);
					svd.setValue(vdc.getValue());
					CommonUtil.processBroadcast(getActivity(), svd);
				}
				// obd信息
				else if (action != null
						&& Constants.MAIN_OBD_ACTION.equals(action)) {

					svd = gson.fromJson(
							intent.getStringExtra(Constants.MAIN_OBD_ACTION),
							SpeechVoiceData.class);
					vdc = gson.fromJson(svd.getValue(), VoiceDataClass.class);

					svd.setCommand(Constants.MAIN_OBD_COM);
					svd.setValue(vdc.getValue());
					CommonUtil.processBroadcast(getActivity(), svd);
				}
				// obd界面导航
				else if (action != null
						&& Constants.MAIN_NAVI_ACTION.equals(action)) {

					svd = gson.fromJson(
							intent.getStringExtra(Constants.MAIN_NAVI_ACTION),
							SpeechVoiceData.class);

					svd.setCommand(Constants.MAIN_NAVI_COM);

					Log.i(TAG,
							intent.getStringExtra(Constants.MAIN_NAVI_ACTION));
					CommonUtil.processBroadcast(getActivity(), svd);
				}
				// 音乐功能
				else if (action != null
						&& Constants.MAIN_MUSIC_ACTION.equals(action)) {
					svd = gson.fromJson(
							intent.getStringExtra(Constants.MAIN_MUSIC_ACTION),
							SpeechVoiceData.class);
					vdc = gson.fromJson(svd.getValue(), VoiceDataClass.class);
					Intent intent2 = new Intent(getActivity(),
							MusicService.class);
					// 停止播放
					if (vdc.getType().equals(Constants.F_R_MUSIC_STOP_PLAY[0])) {
						intent2.putExtra("play", "pause");
						getActivity().startService(intent2);
					}
					// 继续播放
					else if (vdc.getType().equals(
							Constants.F_R_MUSIC_CONTINUE_PLAY[0])) {
						intent2.putExtra("play", "playing");
						intent2.putExtra("id", sp.getInt("currentMusic", 0));
						getActivity().startService(intent2);
					}
					// 下一首
					else if (vdc.getType().equals(
							Constants.F_R_MUSIC_NEXT_MUSIC_PLAY[0])) {
						intent2.putExtra("play", "next");
						intent2.putExtra("id", sp.getInt("currentMusic", 0) + 1);
						getActivity().startService(intent2);
					}
					// 上一首
					else if (vdc.getType().equals(
							Constants.F_R_MUSIC_LAST_MUSIC_PLAY[0])) {
						int index = sp.getInt("currentMusic", 0) - 1;
						if (index < 0) {
							index = 0;
						}
						intent2.putExtra("play", "last");
						intent2.putExtra("id", index);
						getActivity().startService(intent2);
					} else {
						svd.setCommand(Constants.MAIN_MUSIC_COM);

						Log.i(TAG, intent
								.getStringExtra(Constants.MAIN_MUSIC_ACTION));
						CommonUtil.processBroadcast(getActivity(), svd);
					}
				}else if(Constants.INPUT_DATA.equals(action))
				{
					String newMsg = intent.getStringExtra(Constants.MSG_PARAM);
					txt_inputtest.setText(newMsg);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	/**
	 * 依据数据更新OBD的显示界面
	 */
	private void updateOBDView(boolean isLeft, int controlId, String data,
			String desc) {
		// LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
		// LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); //先设置WeightSum
		// ll_left.setWeightSum(3);
		// RelativeLayout.LayoutParams rlp = new
		// RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		// RelativeLayout newrl = new RelativeLayout(getActivity());
		// // newrl.setLayoutParams(rlp);
		// TextView newtvtip = new TextView(getActivity());
		// RelativeLayout.LayoutParams rlptip = new
		// RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		// newtvtip.setLayoutParams(rlptip);
		// newtvtip.
		// TextView newtvcontent = new TextView(getActivity());
		int controlId2 = 0;
		if (hmControlDes.keySet().contains(desc))
			controlId2 = hmControlDes.get(desc);
		try {
			((TextView) getActivity().findViewById(controlId2)).setText(data);
		} catch (Exception e) {
			View newl = LayoutInflater.from(getActivity()).inflate(
					R.layout.item_obddata, null);
			TextView newtvtip = (TextView) newl.findViewById(R.id.tv_datatip);
			TextView newtvcontent = (TextView) newl
					.findViewById(R.id.tv_datacontent);
			newtvcontent.setId(controlId);
			newtvtip.setText(desc);
			newtvcontent.setText(data);
			hmControlDes.put(desc, controlId);
			if (isLeft) {
				ll_left.addView(newl);
			} else {
				ll_right.addView(newl);
			}
		}

	}

	/**
	 * 依据新的数据来更新OBD的数据,需要将ID和
	 */
	private void updateOBDData() {

	}

	// 当fragment被被替换时，这是最后一个被调用的方法
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}
	/**
	 * 发送广播，在界面上进行修改mainActivity
	 */
	public void processBroadcast() {
		Gson gson = new Gson();
		DataCmdMessageClass dcmc = new DataCmdMessageClass();
		dcmc.setCmd(Constants.INPUT_DATA);
		dcmc.setContent("");
		String str = gson.toJson(dcmc);
		PMessage pm = new PMessage();
		pm.command = Constants.TYPE_TEXT;
		pm.message = str;
		pm.fromname = "a";
		pm.toname = "b";
		String str2 = gson.toJson(pm);
		Intent intentAction = new Intent(Constants.MSG_SEND);
		intentAction.putExtra("NMESSAGE", str2);
		Log.d(TAG2,str2);
		getActivity().sendBroadcast(intentAction);
	}
}
